// vite文档
const layouts = import.meta.globEager('../layouts/*.vue');
const views = import.meta.globEager('../views/**/*.vue');

import baseConfig from "../config";

function getRoutes(){

    const layoutRoutes = [];
     
    Object.entries(layouts).forEach(([file,module]) => {

        const route = getRouteByModule(file,module);

        route.children = getChildrenRoutes(route)
        layoutRoutes.push(route);
    })

    return layoutRoutes;
}

function getChildrenRoutes(layoutRoute){

    const routes = [];

    Object.entries(views).forEach(([file,module]) => {

        if(file.includes(`../views/${layoutRoute.name}`)){
            const route = getRouteByModule(file,module);
            routes.push(route);
        }
    })

    return routes;
}

//  获取布局路由的子路由
function getRouteByModule( file,module){


    const name = file.replace(/.+layouts\/|.+views\/|\.vue/gi,'');

    // function sleep(time){
    //     var timeStamp = new Date().getTime();
    //     var endTime = timeStamp + time;
    //     while(true){
    //     if (new Date().getTime() > endTime){
    //      return;
    //     } 
    //     }
    //    }

    // sleep(50000);

    // {
    //     path:'/',
    //     name:'home',
    //     component:() => import('@/views/home.vue'),
    // }, 
    const route = {
        name:name.replace('/','.'),
        path : `/${name}`,
        component:module.default
    }


    return Object.assign(route,module.default?.route);
}

const routes = baseConfig.router_autoload ? getRoutes() : [];

export default routes;